import { isArr, questionTypeSelect } from '../../utils/util'

Component({
  properties: {
    type: Number,
    answers: {
      type: Array,
      default: () => []
    },
    answerResult: {},
    showAnswer: Boolean
  },

  data: {
    selectIds: [],
    typeSelect: questionTypeSelect,
    inputValue: ''
  },

  methods: {
    selectChange (e) {
      let { value } = e.detail
      if (!isArr(value)) value = [value]
      this.setData({
        selectIds: value.map(_ => +_)
      })
      this.triggerEvent('change', { value }, {})
    },

    inputBlur (e) {
      this.setData({
        inputValue: e.detail.value
      })
      this.triggerEvent('change', e.detail, {})
    },

    clearInput () {
      this.setData({
        inputValue: ''
      })
    },

    updateUserSelected (answerList) {
      const arr = answerList && answerList.length ? answerList : [{}]
      if (this.properties.type ===  questionTypeSelect.QA) {
        this.setData({
          inputValue: arr[0].answerContent || ''
        })
      } else {
        this.setData({
          selectIds: answerList
            .map(a => {
                const item = this.properties.answers.find(b => b.choice === a.answerChoice)
                return item && item.id
              })
            .filter(_ => _)
        }, () => {
          console.log('selected: ', this.data.selectIds)
        })
      }
    }
  }
})
